#include "PainterWidget.h"
#include <QLabel>
#include <QVBoxLayout>
#include <QEvent>
#include <QPainter>
#include <QDebug>
#include <QRandomGenerator>

#define PADDING 50
#define INCREAMENT 8 // 一度对应的像素
#define POINT_RADIUS 4 // 点的大小
#define TEXT_OFFSET_X 12 // 文本相对于点的偏移
#define TEXT_OFFSET_Y 12 // 文本相对于点的偏移

PainterWidget::PainterWidget(QWidget *parent)
	: QWidget{parent}
{
	QVBoxLayout * mainLayout=new QVBoxLayout();
	mainLayout->setSpacing(2);
	// 高温曲线
	highLbl = new QLabel();
	highLbl->setStyleSheet("background-color: gray");
	highLbl->setFrameStyle(QFrame::Box);
	// 低温曲线
	lowLbl = new QLabel();
	lowLbl->setStyleSheet("background-color: gray");
	lowLbl->setFrameStyle(QFrame::Box);

	highLbl->installEventFilter(this);
	lowLbl->installEventFilter(this);

	updateTemp(); // 初始化温度

	mainLayout->addWidget(highLbl);
	mainLayout->addWidget(lowLbl);
	this->setLayout(mainLayout);
}

void PainterWidget::PaintHigh()
{
	QPainter painter(highLbl);
	painter.setRenderHint(QPainter::Antialiasing, true);  // 抗锯齿
	// 1. 计算X轴
	int pointX[7] = {0};

	for(int i=0; i<7; ++i){
		pointX[i] = highLbl->pos().x() + PADDING \
					+ (highLbl->width()-2*PADDING) / 6 * i;
	}

	// 2. 计算Y轴
	//	 2.1 计算平均值
	int tempSum = 0;
	for(int i=0; i<7; ++i){
		tempSum += mHighTemp[i];
	}
	int tempAverage = tempSum / 7; // 平均温度
	//   2.2 计算Y轴坐标
	int pointY[7] = {0};
	int yCenter = highLbl->height() / 2; // 水平中点
	int increament = highLbl->height() / 20; // 每一度对应的像素
	for(int i=0; i<7; ++i){
		pointY[i] = yCenter - ((mHighTemp[i] - tempAverage) * increament);
	}

	// 3. 开始绘制
	//   3.1 初始化画笔
	QPen pen=painter.pen();
	pen.setWidth(2);  //设置画笔宽度
	pen.setColor(QColor(255,0,0)); // 设置颜色
	//   3.2 绘制文本、点
	for(int i=0; i<7; ++i){
		painter.drawEllipse(QPoint(pointX[i],pointY[i]),POINT_RADIUS,POINT_RADIUS);
		painter.drawText(QPoint(pointX[i] - TEXT_OFFSET_X,pointY[i] - TEXT_OFFSET_Y), \
						 QString::number(mHighTemp[i]) + "°");
	}
	//   3.3 绘制折线
	for(int i=0; i<6; ++i){
		 if ( i == 0) {
			  pen.setStyle(Qt::DashLine); // 虚线
			  painter.setPen(pen);
		 }else {
			  pen.setStyle(Qt::SolidLine); // 实线
			  painter.setPen(pen);
		 }
		 painter.drawLine(pointX[i],pointY[i],pointX[i+1],pointY[i+1]);
	}
}

void PainterWidget::PaintLow()
{
	 QPainter painter(lowLbl);
	 painter.setRenderHint(QPainter::Antialiasing, true);  // 抗锯齿
	 // 1. 计算X轴
	 int pointX[7] = {0};

	 for(int i=0; i<7; ++i){
		 pointX[i] = lowLbl->pos().x() + PADDING \
					 + (lowLbl->width()-2*PADDING) / 6 * i;
	 }

	 // 2. 计算Y轴
	 //	 2.1 计算平均值
	 int tempSum = 0;
	 for(int i=0; i<7; ++i){
		 tempSum += mLowTemp[i];
	 }
	 int tempAverage = tempSum / 7; // 平均温度
	 //   2.2 计算Y轴坐标
	 int pointY[7] = {0};
	 int yCenter = lowLbl->height() / 2; // 水平中点
	 int increament = lowLbl->height() / 20; // 每一度对应的像素
	 for(int i=0; i<7; ++i){
		 pointY[i] = yCenter - ((mLowTemp[i] - tempAverage) * increament);
	 }

	 // 3. 开始绘制
	 //   3.1 初始化画笔
	 QPen pen=painter.pen();
	 pen.setWidth(2);  //设置画笔宽度
	 pen.setColor(QColor(155,0,255)); // 设置颜色
	 //   3.2 绘制文本、点
	 for(int i=0; i<7; ++i){
		 painter.drawEllipse(QPoint(pointX[i],pointY[i]),POINT_RADIUS,POINT_RADIUS);
		 painter.drawText(QPoint(pointX[i] - TEXT_OFFSET_X,pointY[i] - TEXT_OFFSET_Y), \
						  QString::number(mHighTemp[i]) + "°");
	 }
	 //   3.3 绘制折线
	 for(int i=0; i<6; ++i){
		  if ( i == 0) {
			   pen.setStyle(Qt::DashLine); // 虚线
			   painter.setPen(pen);
		  }else {
			   pen.setStyle(Qt::SolidLine); // 实线
			   painter.setPen(pen);
		  }
		  painter.drawLine(pointX[i],pointY[i],pointX[i+1],pointY[i+1]);
	 }
}

void PainterWidget::updateTemp()
{
	 for ( int i = 0; i < 7; i++ ) {
		  mHighTemp[i] = 20 + QRandomGenerator::global()->generate() % 10;
		  mLowTemp[i] = -5 + QRandomGenerator::global()->generate() % 10;
	 }

	 highLbl->update();
	 lowLbl->update();
}

bool PainterWidget::eventFilter(QObject *watched, QEvent *event)
{
	if(event->type() == QEvent::Paint){
		if(watched == highLbl){
			qDebug() << "high paint";
			PaintHigh();
		}else if(watched == lowLbl){
			qDebug() << "low paint";
			PaintLow();
		}
	}
	if(event->type() == QEvent::MouseButtonPress){
		 updateTemp();
	}
	return QWidget::eventFilter(watched,event);
}
